<template>
    <div id="dv">
        <div>
          <el-form ref="registerForm">
            <el-form-item  prop="phone">
              <el-input placeholder="手机号码" v-model="mobile">
              </el-input>
            </el-form-item>
            <el-form-item  prop="VerificationCode">
              <el-row :gutter="10">
                <el-col :span="15">
                  <el-input placeholder="验证码" v-model="code">
                  </el-input>
                </el-col>
                <el-col :span="9">
                  <el-button :plain="true">获取验证码</el-button>
                </el-col>
              </el-row>
            </el-form-item>
            <el-form-item prop="psw">
              <el-input  placeholder="密码" v-model="inputPassword" :maxlength="20" >
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button   @click.prevent="submitForm('registerForm')">注册</el-button>
            </el-form-item>
          </el-form>         
        </div>
    </div>
</template>


<script>
import { $service } from "@/service/reqService";
import { time } from "@/shared/utils/date.js";
import { myUuid } from "@/shared/utils/uuid.js";

export default {
  name: "Register",
  data() {
    return {
      mobile: "",
      inputPassword: "",
      code: "",
      password: "",
      sourceResult: "",
      saltResult: ""
    };
  },
  methods: {
    submitForm() {
      let _this = this

      // 1.对r_i进行md5
      let r_iMd5Params = {
        source: "RE" + time + myUuid,
        salt: "",
        encode32: false
      };
      var getData1 = $service.funcMd5("secret/md5", r_iMd5Params).then(response => {
        this.sourceResult = response.data.data;
      });

      // 2.对手机号进行md5
      let pswMd5Params = {
          source: this.inputPassword,
          salt: this.mobile,
          encode32: true
      };


      getData1.then(function(){
         return $service.funcMd5("secret/md5", pswMd5Params).then(response => {
          _this.saltResult =  response.data.data
         })
      }).then(function(){
        // 3.aes
        let aesParams = {
          source:_this.saltResult,
          salt: _this.sourceResult,
          encode32: true
        };
        return $service.funcAes("secret/aes", aesParams).then(response => {
          _this.password = response.data.data
        })
      }).then(function(){
        // 4.用户注册
        let data = {
          mobile: _this.mobile,
          code: _this.code,
          password:_this.password
        };
        return $service.insert("customer/register", data).then(response => {
          console.log(response.data);
          _this.$store.commit('user_info',response.data) 
          _this.$router.push({path:'/login'})
        });
      })   
    }
  }
};
</script>

<style scoped>
</style>
